﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLKDS.DTO;

namespace QLKDS.DAL
{
    public class ExportBookManager
    {
        public ExportBookInfo GetExportBookById(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryExportBook = from d in db.ExportBooks
                                   where d.Status.Equals(1)
                                    where d.Id.Equals(id)
                                   select new ExportBookInfo { Id = d.Id, Description = d.Description, CreatedBy = d.CreatedBy, CreatedDate = d.CreatedDate, ModifiedBy = d.ModifiedBy, ModifiedDate = d.ModifiedDate };

                return queryExportBook.First();
            }
        
        }


        public IList<ExportBook> ListExportBook()
        {
            List<ExportBook> listdata=new List<ExportBook>();
            using (var db = new QLKDSDataContext())
            {
                listdata = db.ExportBooks.ToList();
                
            }
            return listdata;
        }
    

        public void CreateExportBook(ExportBook ExportBook)
        {
            using (var db = new QLKDSDataContext())
            {
                db.ExportBooks.InsertOnSubmit(ExportBook);

                db.SubmitChanges();
               
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="exportBook"></param>
        public void UpdateExportBook(ExportBook exportBook)
        {
            using (var db = new QLKDSDataContext())
            {
                var _exportBook = db.ExportBooks.Single(d => d.Id.Equals(exportBook.Id));
                _exportBook.Code = exportBook.Code;
                _exportBook.ExportDate = exportBook.ExportDate;
                _exportBook.TotalAmount = exportBook.TotalAmount;
                _exportBook.CustomerId = exportBook.CustomerId;
                _exportBook.Description = exportBook.Description;
                _exportBook.CreatedBy = exportBook.CreatedBy;
                _exportBook.CreatedDate = exportBook.CreatedDate;
                _exportBook.ModifiedBy = exportBook.ModifiedBy;
                _exportBook.ModifiedDate = exportBook.ModifiedDate;
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteExportBook(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _ExportBook = db.ExportBooks.Single(d => d.Id.Equals(id));

                db.ExportBooks.DeleteOnSubmit(_ExportBook);
                db.SubmitChanges();
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public bool HasCodeExisted(string code)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.ExportBooks
                            where d.Code.Equals(code)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="code"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool HasCodeDuplicated(string code, long id)
        {
            using (var db = new QLKDSDataContext())
            {
                var list = (from d in db.ExportBooks
                            where d.Code.Equals(code)
                            where !d.Id.Equals(id)
                            select d.Id).ToList();

                return list.Count > 0;
            }
        }

     
    }
}
